import sys
sys.path.append("..")
import Cust.leancloudapi
GetApi = Cust.leancloudapi.GetApi()
Press = GetApi.getNews()
mmd = GetApi.getMMD()
aa = GetApi.getAcademyActity()
ct =GetApi.getConservation()
class News:
    def getNews(self):
        query = Press.query()
        query.limit(1000)
        res = query.find()
        news_list = []
        for item in res:
            title = item.get('NewsTitle')
            subtitle = item.get('NewsSubtitle')
            image = item.get('NewsImage')
            content = item.get('NewsContent')
            ntype = item.get('NewsType')
            newsinfo = {'id':item.id,'title':title,'subtitle':subtitle,'image':image,'content':content,'type':ntype}
            news_list.append(newsinfo)
        return news_list
    def insertNews(self,title,subtitle,image,ntype,content,top):
        addNews = Press()
        addNews.set('NewsTitle',title)
        addNews.set('NewsSubtitle',subtitle)
        addNews.set('NewsImage',image)
        addNews.set('NewsType',ntype)
        addNews.set('NewsContent',content)
        addNews.set('NewsTop',top)
        addNews.save()
        return 510

    def modifyNews(self,id,title,subtitle,image,ntype,content):
        updatenews = Press.create_without_data(id)
        updatenews.set('NewsTitle',title)
        updatenews.set('NewsSubtitle',subtitle)
        updatenews.set('NewsImage',image)
        updatenews.set('NewsType',ntype)
        updatenews.set('NewsContent',content)
        try:
            updatenews.save()
            return 530
        except Exception as e:
            print e.message
            return 531
    def delNews(self,id):
         delnews = Press.create_without_data(id)
         try:
             delnews.destroy()
             return 520
         except Exception as e:
             return 521
    def queryNews(self,id):
        query = Press()      
        item = query.create_without_data(id)
        item.fetch()
        title = item.get('NewsTitle')
        subtitle = item.get('NewsSubTitle')
        image = item.get('NewsImage')
        content = item.get('NewsContent')
        ntype = item.get('NewsType')
        newsinfo = {'id':id,'title':title,'subtitle':subtitle,'image':image,'content':content,'type':ntype}         
        return newsinfo

    def insertMMD(self,title,subtitle,image,newsid,classid,starttime,chatid):

        addmmd = mmd()
        news = Press.create_without_data(newsid)
        classinfo = GetApi.getClass()
        clas  = classinfo.create_without_data(classid)
        chatinfo = ct.create_without_data(chatid)
        addmmd.set('Chat',chatinfo)
        addmmd.set('ClassInfo',clas)
        addmmd.set('News',news)
        addmmd.set('Title',title)
        addmmd.set('Subtitle',subtitle)
        addmmd.set('Image',image)
        addmmd.set('StartTime',starttime)
       
        addmmd.save()
        return 910

    def querymmd(self):
        query = mmd.query
        query.limit(1000)
        query.include('ClassInfo')
       
        res = query.find()
        mmd_list = []
        for foo in res:
            id = foo.id
            title = foo.get('Title')
            subtitle = foo.get('Subtitle')
            starttime = foo.get('StartTime')
            clas = foo.get('ClassInfo')
            classname = clas.get('ClassName')
            mmdinfo = {'id':id,'title':title,'subtitle':subtitle,'class':classname,'time':starttime}
            mmd_list.append(mmdinfo)
        return mmd_list
    def delmmd(self,id):
        delmmd = mmd.create_without_data(id)
        try:
            delmmd.destroy()
            return 920
        except Exception as e:
            print e.message
            return 921
    def insertActity(self,academy,newsid):
        addactivity = aa()
        news = Press.create_without_data(newsid)
        addactivity.set('News',news)
        addactivity.set('Academy',academy)
        try:
            addactivity.save()
            return 1010
        except Exception as e:
            print e.message
            return 1011
    def delActivity(self,id):
        delactivity = aa.create_without_data(id)
        try:
            delactivity.destroy()
            return 1020
        except Exception as e:
            return 1021
    def queryactivity(self):
        query = aa.query()
        query.limit(1000)
        query.include('News')
        res = query.find()
        a_list = []
        for item in res:
            id = item.id
            acade = item.get('Academy')
            news = item.get('News')
            title = news.get('NewsSubtitle')
            acadetivity = {'id':id,'academy':acade,'title':title}
            a_list.append(acadetivity)
        return a_list
    def queryCon(self):
        query = ct.query()
        query.limit(1000)
        res = query.find()
        chat_list = []
        for foo in res:
            id = foo.id
            name = foo.get('name')
            chatinfo = {'id':id,'name':name}
            chat_list.append(chatinfo)
        return chat_list
        



